package com.qianji.petmall.order.web;

import com.qianji.common.exception.BizCodeEnum;
import com.qianji.common.exception.NoStockException;
import com.qianji.common.utils.PageUtils;
import com.qianji.petmall.order.service.OrderService;
import com.qianji.petmall.order.vo.OrderConfirmVo;
import com.qianji.petmall.order.vo.OrderResponseVo;
import com.qianji.petmall.order.vo.OrderSubmitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @author liang zai
 * @date 2022/4/24 17:05
 * @description
 */
@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    @GetMapping("toTrade")
    public String toTrade(Model model, HttpServletRequest request) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.toTrade();
        model.addAttribute("orderConfirm",orderConfirmVo);
        return "confirm";
    }
    @GetMapping("/order.html")
    public String getListOrderItem(@RequestParam(value = "pageNum",defaultValue = "1")String number, Model modelAndView){
        Map<String, Object> map = new HashMap<>();
        map.put("page",number);
        PageUtils listOrderItem = orderService.getListOrderItem(map);
//        String item = JSON.toJSONString(listOrderItem);
        modelAndView.addAttribute("listItem",listOrderItem);
//        System.out.println(JSON.toJSONString(listOrderItem));
        return "list";
    }

    @PostMapping("submitOrder")
    public String submitOrder(OrderSubmitVO submitVO, Model model, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        //校验令牌、统计价格、锁库存
        try {
            OrderResponseVo orderResponseVo = orderService.submitOrder(submitVO);
            if (orderResponseVo.getCode()==200){
                //下单成功 跳转支付页
                model.addAttribute("submitOrder",orderResponseVo);
                return "pay";
            }else {
                msg(orderResponseVo);
                redirectAttributes.addFlashAttribute("msg",orderResponseVo.getMessage());
                return "redirect:http://order.petmall.com/toTrade";
                //返回订单页
            }
        }catch (Exception e){
            if (e instanceof NoStockException){
                String msg = "下单失败，商品无库存";
                redirectAttributes.addFlashAttribute("msg", msg);
            }
            return "redirect:http://order.petmall.com/toTrade";
        }
    }

    private void msg(OrderResponseVo orderResponseVo) {
        BizCodeEnum[] values = BizCodeEnum.values();
        for (BizCodeEnum value : values) {
            Integer code = orderResponseVo.getCode();
            if(value.getCode() == code){
                orderResponseVo.setMessage(value.getMsg());
            }
        }
    }
}
